package com.xixibaby.core;
import cn.hutool.core.util.ReflectUtil;

import java.util.*;

/**
 * @author tomj
 * @Date: 2019-02-15 11:01
 * @Description: enum 类
 */
public class EKit<T extends Enum<T>> {
    /**
     * 私有化构造函数  不允许创建
     */
    private EKit(){}
    private Class<T> cls;

    private List<T> list;

    Map<String,Object[]> map = new HashMap<>(8);

    private EKit(Class<T> cls){
        this.cls = cls;
    }

    public static<T extends Enum<T>> EKit<T> model(Class<T> cls){
        return new EKit<T>(cls);
    }

    public<T extends Enum<T>> T get(String pro,Object value){
        Map<Object,T> enumMap = suibianqugemingzi(this.cls,pro);
        return enumMap.get(value);
    }

    public<T extends Enum<T>> T getByCode(Object value){
        return get("code",value);
    }

    public EKit filter(String pro,Object... values){
        this.map.put(pro,values);
        return this;
    }








    /**随便去个名字 反正私有的*/
    private static<T> Map<Object, T> suibianqugemingzi(Class<? extends Enum<?>> clazz, String fieldName) {
        final Enum<?>[] enums = clazz.getEnumConstants();
        if (null == enums) {
            return null;
        }
        final Map<Object, T> map = new HashMap<>(enums.length);
        for (Enum<?> e : enums) {
            map.put(ReflectUtil.getFieldValue(e, fieldName), (T)e);

        }
        return map;
    }

}
